package com.simple.datax.db.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.simple.datax.SimpleMessage;
import com.simple.datax.db.mybatis.client.DataxProcingMapper;
import com.simple.datax.db.mybatis.client.DataxSentMapper;
import com.simple.datax.db.mybatis.model.DataxParseRawMsg;
import com.simple.datax.db.mybatis.model.DataxProcing;
import com.simple.datax.db.mybatis.model.DataxRecvRawMsg;
import com.simple.datax.db.mybatis.model.DataxSendRawMsg;
import com.simple.datax.db.mybatis.model.DataxSent;
import com.simple.datax.db2.mybatis.client.DataxParseRawMsgMapper;
import com.simple.datax.db2.mybatis.client.DataxRecvRawMsgMapper;
import com.simple.datax.db2.mybatis.client.DataxSendRawMsgMapper;
import com.simple.datax.service.SimpleDBException;
import com.simple.datax.utils.SimpleConstants;

/**
 * @Title: AbstractDBServiceImpl.java
 * @Package com.simple.datax.db.service.impl
 * @Description: 数据服务抽象类
 * @author Paladin Chen
 * @date 2012-12-21 下午9:44:59
 * @version V1.0
 */
public abstract class AbstractDBServiceImpl {
	private static final Logger logger = LoggerFactory.getLogger(AbstractDBServiceImpl.class);
	@Autowired
	protected DataxRecvRawMsgMapper drrmm;

	@Autowired
	protected DataxParseRawMsgMapper dprmm;

	@Autowired
	protected DataxSendRawMsgMapper dsrmm;

	@Autowired
	protected DataxSentMapper dsm;

	@Autowired
	protected DataxProcingMapper dpm;

	@Transactional
	public void insertRecvRaw(SimpleMessage im) throws SimpleDBException {
		try {
			DataxRecvRawMsg drrm = new DataxRecvRawMsg();

			drrm.setCommunParam(im.getCommunParam());
			drrm.setCommunType(im.getCommunType());
			drrm.setId(im.getRecvRawMsgid());
			drrm.setMsgData(im.getContent().getBytes());
			drrm.setDataSize(new Long(im.getContent().length()));
			drrm.setRecvDataType(im.getMsgFormat());
			drrm.setRecvFileName(im.getOriFileName(false));
			drrm.setMsgCharset(im.getEncoding());
			drrm.setMsgType(im.getMsgType());

			drrmm.insertSelective(drrm);
		} catch (Exception e) {
			throw new SimpleDBException(e);
		}
	}

	@Transactional
	public void insertParseRaw(SimpleMessage im) throws SimpleDBException {
		try {
			DataxParseRawMsg dprm = new DataxParseRawMsg();

			dprm.setBackupFileName(im.getFileName());
			dprm.setMessageid(im.getMessageid());
			dprm.setMsgType(im.getMsgType());
			dprm.setMsgBusiType(im.getMsgBusiType());

			if (im.isResponse()) {
				dprm.setMsgSubType((short) 1);
			} else {
				dprm.setMsgSubType((short) 0);
			}

			dprm.setDataSize((long) im.getContent().length());
			dprm.setReceiverid(im.getReceiver());
			dprm.setMsgData(im.getContent().getBytes());
			dprm.setSenderid(im.getSender());
			dprm.setRecvRawMsgid(im.getRecvRawMsgid());

			dprmm.insertSelective(dprm);
		} catch (Exception e) {
			throw new SimpleDBException(e);
		}
	}

	@Transactional
	public void insertSentRaw(SimpleMessage im) throws SimpleDBException {
		try {
			DataxSendRawMsg dsrm = new DataxSendRawMsg();

			dsrm.setRecvRawMsgid(im.getRecvRawMsgid());
			dsrm.setSendFileName(im.getFileName());
			dsrm.setMessageid(im.getMessageid());
			dsrm.setDataSize((long) im.getContent().length());
			dsrm.setReceiverid(im.getReceiver());
			dsrm.setMsgData(im.getContent().getBytes());

			dsrm.setSendDataType(im.getSentMsgFormat());

			dsrm.setMsgType(im.getSentMsgType());
			dsrm.setMsgBusiType(im.getMsgBusiType());
            dsrm.setFcSenderId(im.getSender());
            
			if (im.isResponse()) {
				dsrm.setMsgSubType((short) 1);
			} else {
				dsrm.setMsgSubType((short) 0);
			}

			dsrmm.insertSelective(dsrm);
		} catch (Exception e) {
			throw new SimpleDBException(e);
		}
	}

	@Transactional
	public void insertProcing(SimpleMessage im) throws SimpleDBException {
		try {
			DataxProcing dp = new DataxProcing();

			dp.setRecvRawMsgid(im.getRecvRawMsgid());
			dp.setResendCount((short) 0);

			dpm.insertSelective(dp);
		} catch (Exception e) {
			throw new SimpleDBException(e);
		}
	}

	@Transactional
	public void updateProcing(String imMsgid, short status) throws SimpleDBException {
		try {
			DataxProcing dp = new DataxProcing();

			dp.setRecvRawMsgid(imMsgid);
			dp.setStatus(status);

			dpm.updateByPrimaryKeySelective(dp);
		} catch (Exception e) {
			throw new SimpleDBException(e);
		}
	}

	@Transactional
	public void procSent(SimpleMessage im) throws SimpleDBException {
		try {
			short resend = (short) 0;
			// 如果不是内部回复报文，并且原始接收报文id存在，则做删除
			if (!im.isIntraResponse()&&!StringUtils.isBlank(im.getRecvRawMsgid())) {
				try {
					resend = dpm.selectByPrimaryKey(im.getRecvRawMsgid()).getResendCount();
					dpm.deleteByPrimaryKey(im.getRecvRawMsgid());
				} catch (Exception e) {
					logger.error(im.toLog() + " Delete datax Procing Error:", e);
				}
			}

			DataxSent ds = new DataxSent();
			ds.setMessageid(im.getMessageid());
			ds.setRecvRawMsgid(im.getRecvRawMsgid());
			ds.setSendMsgid(im.getSentMessageid());
			
			if(im.getContent() != null){
				ds.setDataSize((long) im.getContent().length());				
			}
			
			ds.setResendCount(resend);
			ds.setSenderid(im.getSender());
			ds.setReceiverid(im.getReceiver());
			ds.setReceiverNode(im.getReceiverNode());
			ds.setSenderNode(im.getSenderNode());
			ds.setServiceCode(im.getServiceCode());

			if (resend > 0) {
				ds.setStatus(SimpleConstants.STATUS_RESENT);
			}

			dsm.insertSelective(ds);
		} catch (Exception e) {
			throw new SimpleDBException(e);
		}
	}
}
